package swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class UserMenuManagment implements ActionListener {
	private JPanel eastPanel;
	private Box box;
	private double sumBlance = 2323.11;
	private AutomaticTellerService automaticTellerService;
	private String sum;
	private JTextField jSum;
	private static long id;
	private static Object[][] data = new Object[4][4];

	public UserMenuManagment(AutomaticTellerService automaticTellerService) {

		this.automaticTellerService = automaticTellerService;
		this.eastPanel = automaticTellerService.getEastPanel();
		this.box = automaticTellerService.getBv();
		automaticTellerService.setPassword("");
		setGUI();
	}

	public void setGUI() {

		JLabel label = (JLabel) box.getComponent(0);
		box.remove(1);
		jSum = new JTextField(3);
		// jSum.addKeyListener(this);
		box.add(jSum);
		jSum.setBackground(Color.white);
		label.setText("Enter sum");
		jSum.setEditable(false);

		JButton btnDraw = new JButton("Draw");
		btnDraw.setMnemonic(KeyEvent.VK_D);
		btnDraw.addActionListener(this);
		box.add(btnDraw);
		box.add(new JLabel("    "));
		box.add(new JLabel("    "));
		box.add(new JLabel(" Your balance : " + getSumBlance()));
		box.add(new JLabel("    "));
		JButton btnHistory = new JButton("History");
		btnHistory.setMnemonic(KeyEvent.VK_H);
		btnHistory.addActionListener(this);
		box.add(btnHistory);
		box.add(new JLabel("    "));
		eastPanel.validate();

	}

	public void setSumBlance(double sumBlance) {
		this.sumBlance = sumBlance;
	}

	public double getSumBlance() {
		return sumBlance;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand().equals("History")) {
			tableOfOpirationCustomer();
		}
		if (e.getActionCommand().equals("Draw")) {
			proccessSum();
		}
	}

	public void proccessSum() {
		sum = jSum.getText();
		System.out.println("Sum recived : " + getSum());
		if (!sum.equals("")) {
			if (drawSum() == true) {
				JOptionPane.showMessageDialog(null,
						"Sum of money was draw succsesfully\n Your Balance is "
								+ getSumBlance() + "", "Warning",
						JOptionPane.WARNING_MESSAGE);
				System.exit(0);
			} else {
				JOptionPane.showMessageDialog(null,
						"You havn't enough money\nPlease enter other sum",
						"Warning", JOptionPane.WARNING_MESSAGE);
				jSum.setText("");
			}
		} else {
			JOptionPane.showMessageDialog(null, "You didn't enter nothing",
					"Error", JOptionPane.ERROR_MESSAGE);
		}

	}

	// this function check if customer have enough money on order to draw it
	// from account
	public boolean drawSum() {

		return true;
	}

	public String getSum() {
		return sum;
	}

	public void setSum(String sum) {
		this.sum = sum;
	}

	public void tableOfOpirationCustomer() {
		JFrame jfTableFrame = new JFrame(String.valueOf(getId()));
		String[] columnsNames = { "Balance ID", "Action", "Amount", "Date" };
		JTable table = new JTable(getData(), columnsNames);
		table.setEnabled(false);
		JScrollPane tablePane = new JScrollPane(table);
		jfTableFrame.add(tablePane);
		jfTableFrame.setSize(new Dimension(400, 200));
		jfTableFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		jfTableFrame.setLocationRelativeTo(null);
		jfTableFrame.setVisible(true);
	}

	public static long getId() {
		return id;
	}

	public static void setId(long id) {
		UserMenuManagment.id = id;
	}

	public static Object[][] getData() {
		return data;
	}

	public static void setData(Object[][] data) {
		UserMenuManagment.data = data;
	}

}
